import HttpRequest from '@/api/request';
import ApiConfig from '@/api/api-config';
import moment from 'moment'

function printPDF(params){
    let url=ApiConfig.getWareHousePDF;
    return HttpRequest.requestBlobGet(url,params)
}

//取画面表格的列配置
function getDispField(data) {
    let url = ApiConfig.getDispField;
    return HttpRequest.requestJsonPost(url, data)
}

// 获取字典项
function getDictListByDictId(data){
    let url = ApiConfig.getDictListByDictId;
    return HttpRequest.requestJsonPost(url, data)
}

function findComponentDownward (context, componentName) {
    const childrens = context.$children;
    let children = [];
    let list = [];
    if(!componentName){
        componentName = 'commonInput'
    }
    if (childrens.length) {
        for (const child of childrens) {
            const name = child.$options.name;

            if (name === componentName) {
                list.push(child);
            } else {
                children = this.findComponentDownward(child, componentName);
                if (children) {
                    list = list.concat(children);
                }
            }
        }
    }
    return list;
}

function validForm(form,typeName){
    if(!typeName){
        typeName = 'commonInput'
    }
    const inputList = this.findComponentDownward(form, typeName);
    let isOk = true;
    for (let input of inputList) {
        let r = input.valid();
        if (!r) {
            isOk = false;
        }
    }
    return isOk;
}

// 转换不规则的日期格式
function convertInvalidDate(strDate) {
    if(!strDate){
        return strDate;
    }
    if (validDate(strDate)) {
        if (strDate.length > 10) {
            return moment(new Date(strDate)).format("YYYY-MM-DD HH:mm:ss")
        } else {
            return moment(new Date(strDate)).format("YYYY-MM-DD")
        }
        // return strDate;
    } else {
        if (strDate.length == 8) {
            let temp = strDate.substring(0, 4) + '-' + strDate.substring(4, 6) + '-' + strDate.substring(6, 8);
            return convertInvalidDate(temp);
        } else if (strDate.indexOf('.') > -1) {
            let temp = strDate.replace(/./g, '-');
            return convertInvalidDate(temp);
        } else if (strDate.indexOf('~') > -1) {
            let temp = strDate.replace(/~/g, '-');
            return convertInvalidDate(temp);
        } else if (strDate.indexOf('年') > -1) {
            let temp = strDate.replace(/年/g, '-');
            temp = temp.replace(/月/g, '-');
            temp = temp.replace(/日/g, '');
            return convertInvalidDate(temp);
        }
    }
}
// 校验日期是否合法
function validDate(strDate){
    if (strDate.length == 8) {
        return false;
    }
    let result = moment(strDate).isValid();
    return result;
}

function getTextWidth(str = '', style) {
    const dom = document.createElement('span');
    dom.style.display = 'inline-block';
    dom.textContent = str;
    // dom.style.fontFamily = style.fontFamily;
    // dom.style.fontWeight = style.fontWeight;
    // dom.style.fontSize = style.fontSize;
    document.body.appendChild(dom);
    const width = dom.clientWidth;
    document.body.removeChild(dom);
    return width;
}

let CommmonService = {
    printPDF,
    getDispField,
    getDictListByDictId,
    findComponentDownward,
    validForm,
    convertInvalidDate,
    getTextWidth
};

export default CommmonService;